<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-21 15:43:52
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-05-26 12:01:20
 */
namespace app\weixin\controller\sys_admin;
use app\AdminController;
use app\weixin\model\WeiXinMpModel;
use app\weixin\model\WeixinVideoCateModel;
/**
 * 视屏号商品类目管理
 * Class Index
 */
class VideoCate extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new WeixinVideoCateModel();
        $this->assign('qualifications',[0=>'不需要',1=>'必填',2=>'选填']);
        $this->assign('exStatus',[0=>'审核中',1=>'<font color="green">已通过</font>',9=>'<font color="red">未通过</font>']);
    }


    /**
     * [videoGoods 视屏号商品类目]
     */
    public function index()
    {
    	$this->getList(true);
    	return $this->fetch('index');
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {

    	$this->Model->getCategoryList();

        $where = [];
        $keyword = input('keyword','','trim');

        if (empty($keyword) == false) {
            $where[] = ['third_cat_name|first_cat_name|second_cat_name','like','%'.$keyword.'%'];
        }
       
        $data = $this->getPageList($this->Model,$where,'id');

        foreach ($data['list'] as $key => $value) {
            $value = $this->Model->info($value);
            $data['list'][$key] = $value;
        }

        
        $this->assign("data", $data);
        if ($runData == false){
            $data['content']= $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }

    /**
     * [add 上传类目]
     */
    public function add()
    {
    	$third_cat_list = $this->Model->getShopCat();

    	if ($this->request->isPost()) {
    		$third_cat_id = input('third_cat_id',0,'intval');
    		if ($third_cat_id <= 0) {
    			return $this->error('请选择分类.');
    		}

    		$cateInfo = $third_cat_list[$third_cat_id];
    		$images = input('images');
    		if ($cateInfo['qualification_type'] == 1 || $cateInfo['product_qualification_type'] == 1) {
    			if (empty($images)) {
    				return $this->error('请上传资质材料.');
    			}
    		}
    		$license = input('license');
    		if (empty($license)) {
    			return $this->error('请上传营业执照.');
    		}
    		$path = $images['path'];
    		$web_path = config('config.host_path');

    		$certificate = [];

    		foreach ($path as $key => $value) {
    			$certificate[] = $web_path.$value;
    		}

    		$thirdCat = $this->Model->where('third_cat_id',$cateInfo['third_cat_id'])->find();

    		if (empty($thirdCat)) {
    			$inArr = [];
	    		$inArr = $cateInfo;
	    		$inArr['license'] = $license;
	    		$inArr['certificate'] = join(',',$path);
	    		$inArr['add_time'] = time();
	    		$res = $this->Model->save($inArr);
	    		if ($res < 1) {
	    			return $this->error('添加失败.');
	    		}
    		}
    		

    		$data = [];
    		$data['audit_req'] = [];
    		$data['audit_req']['license'] = $web_path.$license;
    		$data['audit_req']['category_info']['level1'] = $cateInfo['first_cat_id'];
    		$data['audit_req']['category_info']['level2'] = $cateInfo['second_cat_id'];
    		$data['audit_req']['category_info']['level3'] = $cateInfo['third_cat_id'];
    		$data['audit_req']['category_info']['certificate'] = $certificate;
    		$res = $this->Model->auditCategory($data);
    		$upData = [];
    		if ($res['errcode'] > 0) {
    			if ($res['errcode'] == 1050003) {
    				
    				$upData['update_time'] = time();
    				$upData['status'] = 1;
    				$ress = $this->Model->where('third_cat_id',$cateInfo['third_cat_id'])->update($upData);
    				if ($ress < 1) {
    					return $this->error('添加失败.');
    				}
    				// return $this->success('添加成功.');
    			}
    			return $this->error($res['errmsg']);
    		}

    		$upData['audit_id'] = $res['audit_id'];
    		$upData['update_time'] = time();
    		$upData['status'] = 0;

    		$ress = $this->Model->where('third_cat_id',$cateInfo['third_cat_id'])->update($upData);
    		if ($ress < 1) {
				return $this->error('添加失败.');
			}
            $this->Model->cleanMemcache();
    		return $this->success('添加成功.',url('index'));
    	}



    	$this->assign('third_cat_list',$third_cat_list);
    	return $this->fetch('add');
    }
    /**
     * [info 类目详情]
     * @return [type] [description]
     */
    public function info()
    {
        $id = input('id');
        if ($id < 1) {
            return $this->error('传参错误.');
        }
    	$row = $this->Model->info($id);
        if (is_array($row) == false) {
            return $this->error($row);
        }
    	

    	$this->assign('row',$row);
    	return $this->fetch('info');
    }



    /*------------------------------------------------------ */
    //-- 上传资质图片
    /*------------------------------------------------------ */
    public function uploadImg(){
        $this->returnJson = true;//统一返回json
        $result = $this->_upload($_FILES['file'],'certificate/');
        if ($result['error']) {
            return $this->error('上传失败，请重试.');
        }
        $file_url = str_replace('./','/',$result['info'][0]['savepath'].$result['info'][0]['savename']);
        $data['image'] = array('thumbnail'=>$file_url,'path'=>$file_url);
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 删除图片
    /*------------------------------------------------------ */
    public function removeImg() {
        $file = input('post.url','','trim');
        unlink('.'.$file);
        return $this->success('删除成功.');
    }
}